package com.starmark.core.framework.util;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;

import java.util.List;

/**
 * 简单封装orika, 实现深度转换Bean<->Bean的Mapper.
 */
@SuppressWarnings(value = "unchecked")
public class BeanMapper {

    private static MapperFacade mapper = null;
    private static MapperFactory mapperFactory = null;

    static {
        mapperFactory = new DefaultMapperFactory.Builder().mapNulls(false).build();
        mapper = mapperFactory.getMapperFacade();

    }

    public static MapperFactory getMapperFactory() {
        return mapperFactory;
    }

    /**
     * 基于Dozer转换对象的类型.
     */
    public static <S, D> void map(S source, D destination) {
        mapper.map(source, destination);
    }

    /**
     * 基于Dozer转换对象的类型.
     */
    public static <S, D> D map(S source, Class<D> destinationClass) {
        return mapper.map(source, destinationClass);
    }

    /**
     * 基于Dozer转换Collection中对象的类型.
     */
    public static <S, D> List<D> mapList(Iterable<S> sourceList, Class<D> destinationClass) {
        return mapper.mapAsList(sourceList, destinationClass);
    }

    /**
     * 基于Dozer转换Collection中对象的类型.
     */
    public static <S, D> Page  mapPage(Page<S> sourceList, Class<D> destinationClass) {

        List destinationList = mapper.mapAsList(sourceList.getRecords(), destinationClass);
        sourceList.setRecords(destinationList);
        return sourceList;

    }
}